function Links(linkList) {
   this.linkList = linkList;
}


Links.prototype.addLink = function(rel,href,type,title) {
   var item = document.createElementNS(XUL_NS,"richlistitem");
   item.setAttribute("class","link-item");
   item.addEventListener('click',function() {
      item.focus();
   },true);

   var vbox = document.createElementNS(XUL_NS,"vbox");
   item.appendChild(vbox);
   
   var line1 = document.createElementNS(XUL_NS,"hbox");
   line1.setAttribute("class","href");
   var line2 = document.createElementNS(XUL_NS,"hbox");
   vbox.appendChild(line1);
   vbox.appendChild(line2);
   
   var hrefDisplayE = document.createElementNS(XUL_NS,"label");
   hrefDisplayE.setAttribute("class","plain");
   hrefDisplayE.setAttribute("value",href);
   
   var hrefE = document.createElementNS(XUL_NS,"textbox");
   hrefE.setAttribute("value",href);
   hrefE.setAttribute("class","plain");
   hrefE.setAttribute("hidden","true");
   hrefE.setAttribute("size",""+(href.length+1));
   hrefE.setAttribute("oninput","this.doInput();");
   hrefE.setAttribute("flex","0");
   line1.appendChild(hrefE);
   line1.appendChild(hrefDisplayE);
   
   this._attachChangeHandlers(hrefE,hrefDisplayE);

   var lbracket = document.createElementNS(XUL_NS,"label");
   lbracket.setAttribute("value","[rel=");
   line2.appendChild(lbracket);

   var relE = document.createElementNS(XUL_NS,"textbox");
   relE.setAttribute("class","plain");
   relE.setAttribute("hidden","true");
   if (rel) {
      relE.setAttribute("value",rel);
      relE.setAttribute("size",""+(rel.length+1));
   } else {
      relE.setAttribute("size","1");
   }
   relE.setAttribute("oninput","this.doInput();");
   relE.setAttribute("flex","0");
   line2.appendChild(relE);

   var relDisplayE = document.createElementNS(XUL_NS,"label");
   relDisplayE.setAttribute("class","plain");
   if (rel) {
      relDisplayE.setAttribute("value",rel);
   } else {
      relDisplayE.setAttribute("value"," ");
   }
   line2.appendChild(relDisplayE);
   
   this._attachChangeHandlers(relE,relDisplayE);

   var rbracket = document.createElementNS(XUL_NS,"label");
   rbracket.setAttribute("value","] ");
   line2.appendChild(rbracket);


   var typePrefix = document.createElementNS(XUL_NS,"label");
   typePrefix.setAttribute("value",'[type=');
   line2.appendChild(typePrefix);

   var typeE = document.createElementNS(XUL_NS,"textbox");
   typeE.setAttribute("value",type ? type : "");
   typeE.setAttribute("class","plain");
   typeE.setAttribute("hidden","true");
   typeE.setAttribute("size",""+(type ? type.length+1 : 1));
   typeE.setAttribute("oninput","this.doInput();");
   typeE.setAttribute("flex","0");
   line2.appendChild(typeE);

   var typeDisplayE = document.createElementNS(XUL_NS,"label");
   typeDisplayE.setAttribute("class","plain");
   typeDisplayE.setAttribute("value",type ? type : " ");
   line2.appendChild(typeDisplayE);
   
   this._attachChangeHandlers(typeE,typeDisplayE);

   var typeSuffix = document.createElementNS(XUL_NS,"label");
   typeSuffix.setAttribute("value",'] [title=');
   line2.appendChild(typeSuffix);

   var titleE = document.createElementNS(XUL_NS,"textbox");
   titleE.setAttribute("value",title ? title : "");
   titleE.setAttribute("class","plain");
   titleE.setAttribute("hidden","true");
   titleE.setAttribute("size",""+(title ? title.length+1 : 1));
   titleE.setAttribute("oninput","this.doInput();");
   titleE.setAttribute("flex","0");
   line2.appendChild(titleE);

   var titleDisplayE = document.createElementNS(XUL_NS,"label");
   titleDisplayE.setAttribute("class","plain");
   titleDisplayE.setAttribute("value",title ? title : " ");
   line2.appendChild(titleDisplayE);
   
   this._attachChangeHandlers(titleE,titleDisplayE);

   var titleSuffix = document.createElementNS(XUL_NS,"label");
   titleSuffix.setAttribute("value",']');
   line2.appendChild(titleSuffix);
   
   this.linkList.appendChild(item);
}

Links.prototype._attachChangeHandlers = function(inputE,inputDisplayE) {
   inputE.doInput = function() {
      inputE.size = inputE.value.length+1;
      if (this.timer) {
         clearTimeout(this.timer);
         this.timer = null;
      }
      var current = this;
      this.timer = setTimeout(function() {
         current.onblur();
      },20*1000);
   };
   inputDisplayE.onclick = function() {
      inputDisplayE.hidden = true;
      inputE.hidden = false;
      inputE.inputField.onblur = function() {
         inputDisplayE.value = inputE.value.length>0 ? inputE.value : " ";
         inputE.hidden = true;
         inputDisplayE.hidden = false;
         if (inputE.timer) {
            clearTimeout(inputE.timer);
            inputE.timer = null;
         }
      };
      
      inputE.inputField.focus();
      var current = inputE;
      inputE.timer = setTimeout(function() {
         current.inputField.onblur();
      },20*1000);
   };
}

Links.prototype.deleteSelected = function() {
   var index = this.linkList.selectedIndex;
   var item = this.linkList.selectedItem;

   this.linkList.removeChild(item);
}

Links.prototype.getLinks = function() {
   var set = [];
   var len = this.linkList.getRowCount();
   for (var i=0; i<len; i++) {
      var item = this.linkList.getItemAtIndex(i);
      var nl = item.getElementsByTagNameNS(XUL_NS,"textbox");
      var href = nl.item(0).value+""
      var rel = nl.item(1).value+"";
      var type = nl.item(2).value+"";
      var title = nl.item(3).value+"";
      rel.trim();
      href.trim();
      if (type.length==0) {
         type = null;
      }
      if (title.length==0) {
         title = null;
      }
      if (href.length>0) {
         set.push({ rel: rel, href: href, type: type, title: title});
      }
   }
   return set;
}

Links.prototype.clear = function() {
   var len = this.linkList.getRowCount();
   for (var i=0; i<len; i++) {
      this.linkList.removeChild(this.linkList.getItemAtIndex(0));
   }
}

